package com.tongji.seller.view.comp
{
	import alternativa.protocol.codec.NullMap;
	
	import com.tongji.seller.events.ChangePropertyEvent;
	import com.tongji.seller.events.FilterEvent;
	import com.tongji.seller.events.utilsEvent.BrightEvent;
	import com.tongji.seller.events.utilsEvent.BubblesEvent;
	import com.tongji.seller.model.FurnitureModel;
	import com.tongji.seller.model.StoreProductModel;
	import com.tongji.seller.utils.Blending;
	import com.tongji.seller.utils.ConstVariableInventory;
	import com.tongji.seller.view.twoD.AddBitmap;
	import com.tongji.seller.view.twoD.Furniture;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.Alert;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.events.FlexMouseEvent;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
		
	/**
	 * 
	 * @author yanghanbing
	 * 2011/08/
	 */ 
	public class WorkCompMediator extends Mediator
	{
		[Inject]
		public var view:WorkComp;
		
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		[Inject]
		public var storeProductModel:StoreProductModel;
				
		[Bindable]
		private var room:Furniture;
		

		override public function onRegister():void
		{
			furnitureModel.selectedFur = null;
			eventMap.mapListener(view, DragEvent.DRAG_ENTER, viewDragEnterHandler, DragEvent);
			eventMap.mapListener(view, DragEvent.DRAG_DROP, viewDragDropHandler, DragEvent);
			eventMap.mapListener(view, DragEvent.DRAG_OVER, viewDragOverHandler, DragEvent);
			view.addEventListener(MouseEvent.MOUSE_DOWN, quitEditHandler);
			addViewListener(ChangePropertyEvent.CHANGE_FURNITURE, furnitureClickHandler, ChangePropertyEvent);
			/*this.addContextListener(ChangePropertyEvent.ENLARGE_FURNITURE, enlargePic, ChangePropertyEvent);
			this.addContextListener(ChangePropertyEvent.SHRINK_FURNITURE, shrinkPic, ChangePropertyEvent);
			this.addContextListener(ChangePropertyEvent.DELETE_FURNITURE, deletePic, ChangePropertyEvent);
			this.addContextListener(ChangePropertyEvent.SWITCH_FURNITURE, switchPicHandler, ChangePropertyEvent);	*/
			this.addContextListener(FilterEvent.SET_FILTER, setFilterHandler, FilterEvent);	
			this.addContextListener(BrightEvent.SET_BRIGHT, setBrightHandler, BrightEvent);	
			eventMap.mapListener(view, ChangePropertyEvent.SHRINK_FURNITURE, test, ChangePropertyEvent);
		}
		
		protected function testhandler(event:MouseEvent):void
		{
			event.target;
			event.currentTarget;
		}
		
		protected function quitEditHandler(event:MouseEvent):void
		{			
			for (var i:int = 0; i < view.numChildren; i++)
			{
				if (view.getChildAt(i) is Furniture)
				{
					if (event.target.parent == furnitureModel.selectedFur)
					{
						//furnitureModel.selectedFur = view.getChildAt(i) as Furniture;
					}
					else
					{
						if (furnitureModel.selectedFur != null)
						{ 	
							furnitureModel.selectedFur.setDotShow(false);
							furnitureModel.selectedFur = null;
						}	
					}
				}
				
					
			}
		/*	if (event.target.parent == Sprite)
			{
				
			}
			else
			{
				if (furnitureModel.selectedFur != null)
				{ 	
					furnitureModel.selectedFur.setDotShow(false);
					furnitureModel.selectedFur = null;
				}	
			}*/	
		}
		
		private function test(event:ChangePropertyEvent):void
		{
			Alert.show("ok");
		}
		
		private function viewDragEnterHandler(event:DragEvent):void
		{
			DragManager.acceptDragDrop(IUIComponent(event.target));
		}
		
		private function viewDragDropHandler(event:DragEvent):void
		{
			room = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Furniture;
			
			room.scaleX = 0.36;
			room.scaleY = 0.36;
			room.explicitWidth = room.content.width * room.scaleX;
			room.explicitHeight = room.content.height * room.scaleY;
			
			// 将家具拖到舞台时首次检查是否出界
			var upOut:Boolean = false;
			var downOut:Boolean = false;		
			var leftOut:Boolean = false;
			var rightOut:Boolean = false;
			
			//四周是否超界
			/*upOut = event.localY < room.explicitHeight;
			downOut = (event.localY + room.explicitHeight) > ConstVariableInventory.RECT_HEIGHT;
			leftOut = event.localX < room.explicitWidth;
			rightOut = (event.localX + room.explicitWidth) > ConstVariableInventory.RECT_WIDTH;*/
			
			if (upOut || downOut || leftOut || rightOut)
				Alert.show("不能拖到此处！");
			else
			{		
				furnitureModel.furArray.addItem(room);
				view.addChild(room);
				view.numChildren;
				room.x = view.mouseX;
				room.y = view.mouseY;
				
				/*var backadd:AddBitmap = view.getChildAt(0) as AddBitmap;
				var backbitmap:BitmapData = backadd.addBitmapData;*/	
				//room.setColor(Blending.blending(backbitmap, room.content, room.x, room.y));	
			}
		}
		
		private function viewDragOverHandler(event:DragEvent):void
		{
			room = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Furniture;
			
			//缩放。将家具拖到舞台时其scale值不为1，因此设定explicitWidth为其所显示的宽度
			room.scaleX = ConstVariableInventory.RECT_WIDTH / ConstVariableInventory.PICTURE_WIDTH;
			room.scaleY = ConstVariableInventory.RECT_HEIGHT / ConstVariableInventory.PICTURE_HEIGHT;	
			room.scaleX = 0.36;
			room.scaleY = 0.36;
			room.explicitWidth = room.content.width * room.scaleX;
			room.explicitHeight = room.content.height * room.scaleY;
			
			// 将家具拖到舞台时首次检查是否出界
			var upOut:Boolean = false;
			var downOut:Boolean = false;		
			var leftOut:Boolean = false;
			var rightOut:Boolean = false;
			
			//四周是否超界
			upOut = event.localY < view.y;
			downOut = (event.localY + room.explicitHeight) > ConstVariableInventory.RECT_HEIGHT;
			leftOut = event.localX < view.x;
			rightOut = (event.localX + room.explicitWidth) > ConstVariableInventory.RECT_WIDTH;
			
			if (upOut || downOut || leftOut || rightOut)
				DragManager.showFeedback(DragManager.NONE);
			else
				DragManager.showFeedback(DragManager.MOVE);
		}				
				
		private function furnitureClickHandler(event:ChangePropertyEvent):void
		{
			var locateInArray:int = furnitureModel.furArray.getItemIndex(event.target);
			if (furnitureModel.selectedFur != null)
			{
				if (furnitureModel.selectedFur != event.fur)
				    furnitureModel.selectedFur.setDotShow(false);
			}
			else
				furnitureModel.selectedFur = event.fur;
			
			furnitureModel.selectedFur = event.fur;
			//furnitureModel.resetImges(event.fur.hostProduct);
		}
		
		private function setFilterHandler(event:FilterEvent):void
		{
			furnitureModel.selectedFur.setFilter(event.dropFilter);
			//event.target.setFilter(event.dropFilter);
		}
		
		private function setBrightHandler(event:BrightEvent):void
		{
			furnitureModel.selectedFur.setBright(event.bright);
		}
		/*--------------------------------------------------------
		private function switchPicHandler(event:ChangePropertyEvent):void
		{
			var bitmapdata:BitmapData = storeProductModel.uploadedProductArray[(storeProductModel.arrarIndex - 8) * 3 + storeProductModel.nowIndex % 3];
			
			storeProductModel.nowIndex ++;
			var x:int = event.fur.x;
			var y:int = event.fur.y;
			view.removeChildAt(view.numChildren);
			
			room = new Furniture(view, bitmapdata);
			
			room.x = x;
			room.y = y;
			room.scaleX = 0.36;
			room.scaleY = 0.36;
			room.explicitWidth = room.content.width * room.scaleX;
			room.explicitHeight = room.content.height * room.scaleY;
			
			furnitureModel.furArray.addItem(room);
			view.addChild(room);
			
			if (view.getChildAt(0) is Furniture)
				var backbitmap:BitmapData = (view.getChildAt(0) as Furniture).content;
			else if (view.getChildAt(0) is AddBitmap)
				var backbitmap:BitmapData = (view.getChildAt(0) as AddBitmap).addBitmapData;
			
			room.setColor(Blending.blending(backbitmap, room.content, room.x, room.y));	
			
			//event.fur.content = storeProductModel.uploadedProductArray[storeProductModel.arrarIndex + 1];
			//event.fur.distortSprite.bitmap.bitmapData = storeProductModel.uploadedProductArray[storeProductModel.arrarIndex + 1];
		}
		
		
		public function enlargePic(event:ChangePropertyEvent):void
		{
			//右侧和下方是否超界
			var down:Boolean = false;	
			var right:Boolean = false;
			
			down = (event.fur.y + event.fur.explicitHeight * 1.05) > ConstVariableInventory.RECT_HEIGHT;
			right = (event.fur.x + event.fur.explicitWidth * 1.05) > ConstVariableInventory.RECT_WIDTH;
			
			if (down || right)
				Alert.show("超界！");
			else
			{
				event.fur.scaleX *= 1.05;
				event.fur.scaleY *= 1.05;
				event.fur.explicitWidth = event.fur.content.width * event.fur.scaleX;
				event.fur.explicitHeight = event.fur.content.height * event.fur.scaleY;

				singleFurnitureTitleWindow.x = event.fur.x + event.fur.explicitWidth + ConstVariableInventory.COMP_X / ConstVariableInventory.X_SCALE;
				singleFurnitureTitleWindow.y = event.fur.y + event.fur.explicitHeight + ConstVariableInventory.COMP_Y / ConstVariableInventory.Y_SCALE;
				
			}	
			dispatch(new BubblesEvent(BubblesEvent.DRAG_PRODUCT, event.target));
		}
		
		public function shrinkPic(event:ChangePropertyEvent):void
		{
			event.fur.scaleX *= 0.95;
			event.fur.scaleY *= 0.95;
			event.fur.explicitWidth = event.fur.content.width * event.fur.scaleX * 0.95;
			event.fur.explicitHeight = event.fur.content.height * event.fur.scaleY * 0.95;
			singleFurnitureTitleWindow.x = event.fur.x + event.fur.explicitWidth + ConstVariableInventory.COMP_X / ConstVariableInventory.X_SCALE;
			singleFurnitureTitleWindow.y = event.fur.y + event.fur.explicitHeight + ConstVariableInventory.COMP_Y / ConstVariableInventory.Y_SCALE;		
		}
	
		public function deletePic(event:ChangePropertyEvent):void
		{
			furnitureModel.deleteSelectedObj();
			mediatorMap.removeMediatorByView(singleFurnitureTitleWindow);
			//view.removeChild(event.fur);
			furnitureModel.furniturCount--;
		}
		--------------------------------------------------------*/
	}
}